
let uploadResults = [];
async function chackimg(num) {
    let res = await wx.chooseImage({
        count: num,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
    });



    await uploadImage(res.tempFilePaths)
    return { tempFilePaths: res.tempFilePaths, imgkeylist: uploadResults }
}
async function uploadImage(imgs) {
    uploadResults = [];
    if (!imgs || imgs.length === 0) {
        wx.showToast({
            title: '请先选择图片',
            icon: 'none'
        });
        return;
    }

    wx.showLoading({
        title: `上传中(0/${imgs.length})`,
        mask: true
    });

    try {
        const app = getApp();
        console.log(app)
        console.log("spp")
        // 获取七牛云token
        const res = await app.axios(
            'qiniu',
            {
                op: "qiniu_loadtuken"
            },
            false
        );
        const token = res.data;
        let completedCount = 0;

        // 创建所有上传任务的Promise数组
        const uploadPromises = imgs.map((img, index) => {
            return new Promise((resolve) => {
                wx.uploadFile({
                    url: 'https://up-z2.qbox.me',
                    filePath: img,
                    name: 'file',
                    formData: {
                        token: token
                    },
                    success: (res) => {
                        try {
                            const result = JSON.parse(res.data);
                            uploadResults.push(result.key);

                        } catch (e) {
                            console.error('解析返回数据失败:', e);
                        }
                        completedCount++;
                        wx.showLoading({
                            title: `上传中(${completedCount}/${imgs.length})`
                        });
                        resolve();
                    },
                    fail: (err) => {
                        completedCount++;
                        wx.showLoading({
                            title: `上传中(${completedCount}/${imgs.length})`
                        });
                        console.error(`第${index + 1}个文件上传失败:`, err);
                        resolve(); // 即使失败也继续其他上传
                    }
                });
            });
        });

        // 等待所有上传任务完成
        await Promise.all(uploadPromises);

        // this.setData({
        //     zzimgs: uploadResults
        // });

        wx.hideLoading();
        if (uploadResults.length === imgs.length) {
            wx.showToast({
                title: '全部上传成功',
                icon: 'success',
                duration: 2000
            });
        } else {
            wx.showToast({
                title: `上传成功${uploadResults.length}/${imgs.length}个文件`,
                icon: 'none',
                duration: 2000
            });
        }

    } catch (error) {
        wx.hideLoading();
        wx.showToast({
            title: '上传出错',
            icon: 'none'
        });
        console.error('上传错误:', error);
    }
    return uploadResults
}


module.exports = {
    uploadImage: uploadImage,
    chackimg: chackimg,
}